﻿using System.Collections.Generic;
using System.Linq;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.GamePlay;

namespace IQIGame.Onigao.Logic
{
    public class BuffEffective_AddSkill : BuffEffective
    {
        protected override void OnInitProperty(BuffEffectData rData, BuffComponent rTarget)
        {
            base.OnInitProperty(rData, rTarget);
            if (rData.param.Count > 0)
            {
                rData.userData = new ConfigData()
                {
                    skillID = rData.param[0],
                };
            }
        }

        protected override void OnActive(BuffEffectContext rContext, BuffEffectData rConfig, int nRuntimeParam)
        {
            base.OnActive(rContext, rConfig, nRuntimeParam);
            if (rConfig.userData is ConfigData rConfigData)
            {
                var rTarget = rContext.buff.target;
                var rSkillComp = rTarget.unit.GetComponent<SkillComponent>(ETComponentType.SkillComponent);
                if (rSkillComp == null)
                {
                    return;
                }

                var rSkillConfig = SkillFactory.GetConfig(rConfigData.skillID);
                if (rSkillConfig != null)
                {
                    var rSkill = SkillFactory.CreateSkill(rConfigData.skillID, rSkillConfig, 1,rTarget.unit);
                    if (rSkillComp.AddSkill(rSkill))
                    {
                        rContext.result = rSkill;
                    }
                }
            }
        }

        protected override void OnDeActive(BuffEffectContext rContext, BuffEffectData rConfig)
        {
            base.OnDeActive(rContext, rConfig);
            var rTarget = rContext.buff.target;
            var rSkill = rContext.result as Skill;
            if (rSkill == null)
            {
                return;
            }

            var rSkillComp = rTarget.unit.GetComponent<SkillComponent>(ETComponentType.SkillComponent);
            rSkillComp.RemoveSkill(rSkill);
        }

        public class ConfigData : BuffEffectiveConfig
        {
            public int skillID;
        }

#if UNITY_EDITOR
        public override string ToString(BuffEffectData rConfig)
        {
            return $"添加技能{(rConfig.userData as ConfigData)?.skillID}";
        }
#endif
    }
}